package com.robot.base.configs;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.robot.base.model.LoginBean;
import com.robot.base.model.UserInfoBean;
import com.robot.base.util.arouter.ARouterUtil;
import com.tencent.mmkv.MMKV;

import java.util.UUID;

public class PrefsManager {

    @SuppressLint("CommitPrefEdits")
    public static void load(Context a) {
        try {
            MMKV.initialize(a);
        } catch (Exception e) {
        }
    }

    public static void clear() {
        MMKV.defaultMMKV().clear();
    }

    public static void logout() {
        clear();
        ActivityUtils.finishAllActivities();
        ARouterUtil.INSTANCE.goToActivity(AppConstants.Router.Main.A_LOGIN);
    }

    public static void saveUserLoginInfo(LoginBean data) {
        MMKV.defaultMMKV().encode(AppConstants.SpKey.USERILOGININFO, JSON.toJSONString(data));
    }

    public static LoginBean getUserLoginInfo() {
        String ss = MMKV.defaultMMKV().decodeString(AppConstants.SpKey.USERILOGININFO, null);
        if (!TextUtils.isEmpty(ss)) {
            return JSON.parseObject(ss, LoginBean.class);
        } else {
            return new LoginBean();
        }
    }

    public static void saveUserInfo(UserInfoBean bean) {

        MMKV.defaultMMKV().encode(AppConstants.SpKey.USERINFO, JSON.toJSONString(bean));
    }

    public static UserInfoBean getUserInfo() {
        String ss = MMKV.defaultMMKV().decodeString(AppConstants.SpKey.USERINFO, null);
        if (!TextUtils.isEmpty(ss)) {
            return JSON.parseObject(ss, UserInfoBean.class);
        } else {
            return new UserInfoBean();
        }
    }

    public static String getUUid() {
        String uuid = MMKV.defaultMMKV().decodeString(AppConstants.SpKey.UUID);
        if (TextUtils.isEmpty(uuid)) {
            uuid = UUID.randomUUID().toString().replace("-", "");
            MMKV.defaultMMKV().encode(AppConstants.SpKey.UUID, uuid);
        }
        LogUtils.e(uuid);
        return uuid;
    }
}
